// pages/studyWord/reviewWord/reviewWord.js
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    okNums: 0,
    errNums: 0,
    selNums: {},
    ansNums: {},
    innerAudioContext: new Object(),
    allWord: '',
    canWord:[], //累计会的单词
    errWord: [],//累计不会的词
    can_notString: "",//不会的词的id字符串
    questData: [],
    wordID: 0,
    wordLen: 0,
    readTimes: {},
    packageid: "",
    page: 1,//分页 第一页是1
    pageSize: 200,
    hasMoreData: false,
    boxnum: 1, //盒子num
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    //获取到传过来的包id
    this.data.packageid = options.packageid;
    //获取到传过来的盒子num
    let boxnum = options.boxNum;
    that.setData({
      boxnum: boxnum
    })

    let unitnum = boxnum - 1;
    //设置title
    wx.setNavigationBarTitle({
      title: 'Unit ' + unitnum,
    })

    //获取盒子单词
    that.getBoxWords(boxnum);
  },

    //获取盒子里的单词
    getBoxWords: function (boxnum) {
      console.log("调查看某个盒子单词的接口，盒子：" + boxnum)
      let that = this;
      wx.showLoading({
        title: '',
        mask: true
      })
      httpUtils.getBoxWords({
        access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
        package_id: that.data.packageid,
        square_type: boxnum,
        page: that.data.page,
      }).then((res) => {
        // that.setData({
        //   questData: res.data,
        // })

        var questData = that.data.questData;
        var list = res.data;
        if (that.data.page == 1) {
          questData = []
        }
        if (list.length < that.data.pageSize) {
          that.setData({
            questData: questData.concat(list),
            hasMoreData: false
          })
        } else {
          that.setData({
            questData: questData.concat(list),
            hasMoreData: true,
            page: that.data.page + 1,
          })
        }
  
      });
    },

/**
 * 提交复习的词
 */
  submitData: function () {
    let that = this;
    that.referWord();
  },

  /**
    * 提交单词 点击下一页和开始识记都需要提交
    */
  referWord: function () {
    let that = this;
    //单词id以，隔开的字符串
    var canString = "";
    var can_notString = "";
    console.log("ansNum:"+JSON.stringify(this.data.ansNums))
 
    for (var key in that.data.ansNums) {
      if (that.data.ansNums[key] == 1) {
        if (key < that.data.questData.length) {
          canString += that.data.questData[key].id+",";
        }
      }else if(that.data.ansNums[key] == 2){
        if (key < that.data.questData.length) {
          can_notString += that.data.questData[key].id+",";
        }
      }
    }

    console.log("canString:" + canString);
    console.log("notcanstring:" + can_notString)
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.referWord({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
      type: "review",
      times: "2",
      f_type: "1",
      can: canString,
      can_not: can_notString,
    }).then((res) => {
      console.log(res);
      wx.showToast({
        title: "提交成功",
        icon: 'success',
        duration: 1000
      });
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 1000)

    });
  },

  clickErr: function (e) {
    let temWrod = e.currentTarget.dataset.words;
    let temA = this.data.ansNums;
    let errNums = this.data.errNums;
    let okNums = this.data.okNums;
    if (temA[temWrod] == 1) {
      okNums--;
    }
    if (temA[temWrod] != 2) {
      errNums++;
    }
    temA[temWrod] = 2;

    this.setData({
      ansNums: temA,
      errNums: errNums,
      okNums: okNums
    })
  },
  clickOk: function (e) {
    // console.log("-----------------", JSON.stringify(e));
    let temWrod = e.currentTarget.dataset.words;
    let errNums = this.data.errNums;
    let okNums = this.data.okNums;
    let temA = this.data.ansNums;
    if (temA[temWrod] == 2) {
      errNums--;
    }

    if (temA[temWrod] != 1) {
      okNums++;
    }

    temA[temWrod] = 1;
    this.setData({
      ansNums: temA,
      errNums: errNums,
      okNums: okNums
    })

  },
  clickTxt: function (e) {
    this.data.innerAudioContext.stop();
    let temWrod = e.currentTarget.dataset.words;
    let temNum = this.data.selNums;
    let audiosrc=e.currentTarget.dataset.audios;
    this.data.innerAudioContext.src = audiosrc;
    this.data.innerAudioContext.play();
    // if (temNum[temWrod] === undefined) {
    //   temNum[temWrod] = 2;
    //   this.setData({
    //     selNums: temNum,
    //     word1Cls: 'xhjc_text3_2'
    //   });
    // } else {
    //   let rTimes = this.data.readTimes;
    //   rTimes[temWrod] = 1;
    //   if (temNum[temWrod] == 1) {
    //     temNum[temWrod] = 2;
    //   } else {
    //     temNum[temWrod] = 1;
    //   }
    var isHas = false;//是否点击过 false没有点击过
    for (var key in temNum) {//0正常样式1英文变粗 2英文变粗出中文
      if (key == temWrod) {
        if (temNum[key] < 2) {
          temNum[key]++;
        } else {
          temNum[key] = 1;
        }
        isHas = true;
      } else {
        temNum[key] = 0;
      }
    }
    if (!isHas) {
      temNum[temWrod] = 1;
    }
    this.setData({
      selNums: temNum,
      // readTimes: rTimes
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("onReachBottom方法触发了")
    var that = this;
    let boxnum =that.data.boxnum;
    if (that.data.hasMoreData) {
      that.getBoxWords(boxnum);
      console.log("请求下一页数据")
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    }
  },

})